﻿Imports Microsoft.Win32
Public Class OpenVPNLogging
#Region "Logging"

    Shared Sub WLog(Message As String, EventType As EventLogEntryType)
        WDbg(Message)
        WriteToEventLog(Message, , EventType)
    End Sub

    Shared Sub WDbg(Message As String)
        Trace.WriteLine("OpenVPNWinSvc: " & Message)
    End Sub

    Shared Sub WriteToEventLog(ByVal entry As String, _
                Optional ByVal appName As String = "OpenVPNWinSvc", _
                Optional ByVal eventType As  _
                EventLogEntryType = EventLogEntryType.Information, _
                Optional ByVal logName As String = "OpenVPN Service for Windows")

        Dim objEventLog As New EventLog

        Try

            'Register the Application as an Event Source
            If Not EventLog.SourceExists(appName) Then
                EventLog.CreateEventSource(appName, logName)
            End If

            'log the entry
            objEventLog.Source = appName
            objEventLog.WriteEntry(entry, eventType)

        Catch Ex As Exception

            ' can't log - crash service
            Throw Ex

        Finally
            objEventLog.Close()
        End Try
    End Sub
#End Region
End Class
